package cn.tonyandmoney.voicenotes.alarm.utils

import java.text.SimpleDateFormat
import java.util.*

object AlarmUtils {

    fun dateFormat(date: Date): String {
        val simpleDateFormat = SimpleDateFormat("HH:mm", Locale.CANADA)
        return simpleDateFormat.format(date)
    }

    fun parseDate(date: String): Date {
        val simpleDateFormat = SimpleDateFormat("HH:mm", Locale.CANADA)
        return simpleDateFormat.parse(date)
    }

    fun getTimesFromString(date: String): List<Int> {
        return date.split(":".toRegex())
                .dropLastWhile({ it.isEmpty() })
                .map { Integer.valueOf(it) }
    }

    /**
     * @param repeat 解析二进制闹钟周期
     * @param flag   flag=0返回带有汉字的周一，周二cycle等，flag=1,返回weeks(1,2,3)
     * @return
     */
    fun parseRepeat(repeat: Int, flag: Int): String {
        if (repeat == 0) {
            return "每天"
        } else if (repeat == -1) {
            return "只响一次"
        }
        var repeat = repeat
        var cycle = ""
        var weeks = ""
        if (repeat == 0) {
            repeat = 127
        }
        if (repeat % 2 == 1) {
            cycle = "周一"
            weeks = "1"
        }
        if (repeat % 4 >= 2) {
            if ("" == cycle) {
                cycle = "周二"
                weeks = "2"
            } else {
                cycle = "$cycle,周二"
                weeks = "$weeks,2"
            }
        }
        if (repeat % 8 >= 4) {
            if ("" == cycle) {
                cycle = "周三"
                weeks = "3"
            } else {
                cycle = "$cycle,周三"
                weeks = "$weeks,3"
            }
        }
        if (repeat % 16 >= 8) {
            if ("" == cycle) {
                cycle = "周四"
                weeks = "4"
            } else {
                cycle = "$cycle,周四"
                weeks = "$weeks,4"
            }
        }
        if (repeat % 32 >= 16) {
            if ("" == cycle) {
                cycle = "周五"
                weeks = "5"
            } else {
                cycle = "$cycle,周五"
                weeks = "$weeks,5"
            }
        }
        if (repeat % 64 >= 32) {
            if ("" == cycle) {
                cycle = "周六"
                weeks = "6"
            } else {
                cycle = "$cycle,周六"
                weeks = "$weeks,6"
            }
        }
        if (repeat / 64 == 1) {
            if ("" == cycle) {
                cycle = "周日"
                weeks = "7"
            } else {
                cycle = "$cycle,周日"
                weeks = "$weeks,7"
            }
        }

        return if (flag == 0) cycle else weeks
    }
}